'use strict';
module.exports = app => {
  class AtMeController extends app.Controller {
    // async 异步 是 promise 的 语法糖
    async index() {
      // 获取session
      const { id: userId } = this.ctx.session.userInfo;

      // 1. 获取 atme 的未读数量
      const atCount = await this.service.atRelation.getAtCount(userId);

      // 2. 获取 atme 的微博列表
      const result = await this.service.atRelation.getAtMeBlogList({ userId, pageIndex: 0, pageSize: 5 });

      // console.log(result);
      await this.ctx.render('atMe.html', {
        blogList: result.blogList,
        count: result.count,
        pageIndex: 1,
        pageSize: 5,
        atCount,
      });

      // 将 atme 变成已读
      await this.service.atRelation.update({ newIsRead: true }, { userId, isRead: false });
    }
  }
  return AtMeController;
};
